//
//  LXSelectSpeakerVC.m
//  bgms_d
//
//  Created by Mountain on 29/1/2019.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXSelectSpeakerVC.h"
#import "LXLoginVC.h"

@interface LXSelectSpeakerVC () <BLETransDeviceSingletonDelegate>

@end

@implementation LXSelectSpeakerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"221816"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [BLETransDeviceSingleton sharedInstance].delegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tableHeight.constant = _peripheralArray.count * 120;
}

- (void)setPeripheralArray:(NSMutableArray *)peripheralArray
{
    NSLog(@"23555555:%ld", peripheralArray.count);
    _peripheralArray = peripheralArray;
    [self.tableView reloadData];
}
#pragma mark -  delegate
#pragma mark tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _peripheralArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    UILabel *text = [cell.contentView viewWithTag:1];
    if (!text) {
        text = [[UILabel alloc] initWithFrame:CGRectMake(0, [LXAutoLength lx_L:40], kScreenWidth, [LXAutoLength lx_L:45])];
        text.tag = 1;
        text.font = [UIFont systemFontOfSize:20];
        text.textColor = kAppNormalTextColor;
        text.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:text];
        [cell.contentView drawAroundViewWithLocation:DrawBottom color:kAppGrayTextColor insets:UIEdgeInsetsMake(0, 0, 0.5, 0)];
    }
    CBPeripheral *cbp = [_peripheralArray objectAtIndex:indexPath.row];
    if (cbp.name != nil) {
        text.text = cbp.name;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    CBPeripheral *cbp = [_peripheralArray objectAtIndex:indexPath.row];
    if (cbp != nil) {
        [[BLETransDeviceSingleton sharedInstance] connect:cbp];
    }
}

#pragma mark BLETransDeviceSingletonDelegate
- (void)BLETransDeviceSingletonConnected:(BOOL)connected
{
    if (connected) {
        LXLoginVC *vc = [[LXLoginVC alloc] initWithNibName:NSStringFromClass([LXLoginVC class]) bundle:nil];
        LXThemeColorNavigationController *themeNav = [[LXThemeColorNavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:themeNav animated:YES completion:nil];
    } else {
        [EasyTextView showErrorText:@"Conncet failed"];
    }
}
@end
